package com.hisan.freeride.home.activity;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.View;
import android.widget.TextView;

import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.base.BaseAppLication;
import com.hisan.freeride.common.utils.CacheUtils;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.utils.utils.TokenUtils;
import com.hisan.freeride.common.view.UserLinearLayout;
import com.hisan.freeride.databinding.InstallBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * 设置页面
 * Created by Administrator on 2017/5/5.
 */

public class InstalActivity extends BaseActivity<InstallBinding> {
    private List<UserLinearLayout> lists;
    private String[] names={"紧急联系人","收货地址","常用乘坐人","用户指南","关于顺风车","清除缓存"};
    private Intent intent;


    @Override
    protected int getLayoutId() {
        return R.layout.install;
    }

    @Override
    protected void initView() {
        if (BaseAppLication.getInstance().islogin()) {
            mBinding.loginOut.setVisibility(View.VISIBLE);
        } else {
            mBinding.loginOut.setVisibility(View.GONE);
        }

        lists = new ArrayList<>(5);
        lists.add(mBinding.linkname);
        lists.add(mBinding.address);
        lists.add(mBinding.rideuser);
//        lists.add(mBinding.legal);
        lists.add(mBinding.guide);
        lists.add(mBinding.on);
        lists.add(mBinding.chan);
        initSetheadline(lists);
    }

    @Override
    protected void initEvent() {
        mBinding.topBack.setOnClickListener(view -> finishActivity(true));
        mBinding.linkname.setOnClickListener(v -> {
            String  token= TokenUtils.getToken();
            if (!CollectionUtils.isNullOrEmpty(token)) {
                startActivityForResult(ConnectionActivity.class,0,null,true);
            } else {
                startActivityForResult(LoginActivity.class, 99, null,true);
            }
        } );
        // mBinding.address.setOnClickListener(v -> startActivityForResult(AddressActivity.class,0,null,true));
        mBinding.address.setOnClickListener(v -> showSuccessToast("正在开发,敬请期待！"));
        mBinding.rideuser.setOnClickListener(v -> {
            String  token= TokenUtils.getToken();
            if (!CollectionUtils.isNullOrEmpty(token)) {
                startActivityForResult(RideUserActivity.class,0,null,true);
            } else {
                startActivityForResult(LoginActivity.class, 99, null,true);
            }
        } );
        mBinding.loginOut.setOnClickListener(v -> {getmDialog("是否退出当前账号?","取消","确定").show();});
//        mBinding.legal.setOnClickListener(v -> RotationActivity.skip(InstalActivity.this, AppConfig.legal));
        mBinding.guide.setOnClickListener(v -> RotationActivity.skip(InstalActivity.this, AppConfig.userguide));
        mBinding.on.setOnClickListener(v -> RotationActivity.skip(InstalActivity.this, AppConfig.about));
        mBinding.chan.setOnClickListener(v -> {showSuccessToast("清理成功！");});
    }

    @Override
    protected void loadData(boolean isRefresh) {
        PackageManager pm = getPackageManager();
        try {
            //获取包的详细信息
            PackageInfo info = pm.getPackageInfo(getPackageName(), 0);
            String codename=info.versionName;
            mBinding.myversion.setText("当前版本: "+codename);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onOk() {
        super.onOk();
        new Thread(() -> CacheUtils.getInstance().clear()).start();

        if (CollectionUtils.isNullOrEmpty(intent))
            intent =new Intent();
        BaseAppLication.getInstance().setIslogin(false);
        setResult(99,intent);
        finishActivity(true);
    }

    //循环添加标题内容
    private void initSetheadline(List<UserLinearLayout> lists) {
        TextView classname;
        for (int i=0;i<lists.size();i++){
            classname=lists.get(i).getClassname();
            classname.setText(names[i]);
        }
    }

}
